home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_400
/
440_01
/
!best001.c
next >
Wrap
C/C++ Source or Header
|
1994-05-12
|
28KB
|
720 lines
/*==========================================================================
*
* !BEST001.C Tuesday, April 12, 1994
*
* con_ str_ fil_ counter counter_reg dos_shell
* supplementary source file 1 for The BESTLibrary
*
* Authored independently by George Vanous
*
*==========================================================================*/
/* ------------------------------------------------------------------------ */
/* ---------------------------- INCLUDE FILES --------------------------- */
#include <dir.h>
#include <alloc.h>
#include <errno.h>
#include <stdlib.h>
#include <process.h>
#include "!bestlib.h"
/* ------------------------------------------------------------------------ */
/* ------------------------------ MESSAGES ------------------------------ */
#define MSG01 "\nThis is your first time using %s\nI, %s, hope you enjoy this program\n\n"
#define MSG02 "\n%s has been used %d time"
#define MSG03 "\nThere is %ld bytes of free RAM now available to %s\n"
#define MSG04 "\nThis copy of %s is registered to %s\n"
#define MSG05 "\nThis is the master copy of %s\nIt is registered to its author, %s\n"
#define ERR01 "\n!!! ERROR !!! I cannot find the data file %s\n"
/* ------------------------------------------------------------------------ */
/*----------------------------------------------------------------------------
* Shell to DOS.
*
* RETURNS:
* = pointer to an error message
* = NULL if no error message
*/
char *dos_shell(void)
{
int error;
char *commandpath, *textstring;
struct ffblk ffblk;
if ((commandpath = getenv("COMSPEC")) == NULL &&
(commandpath = searchpath("COMMAND.COM")) == NULL) {
if ((error = findfirst(commandpath = "COMMAND.COM", &ffblk, 0)) == NULL &&
(error = findfirst(commandpath = "C:\COMMAND.COM", &ffblk, 0)) == NULL &&
(error = findfirst(commandpath = "C:\DOS\COMMAND.COM", &ffblk, 0)) == NULL &&
(error = findfirst(commandpath = "C:\BIN\COMMAND.COM", &ffblk, 0)) == NULL) {
textstring = "Cannot find file COMMAND.COM -- initialize COMSPEC=[path]COMMAND.COM";
return( textstring );
}
}
video_set(TEXT);
error = spawnl(P_WAIT, commandpath, commandpath, NULL);
if (error == -1) {
switch(errno) {
case EINVAL : textstring = "Bad argument passed to COMMAND.COM"; break;
case E2BIG : textstring = "Too many arguments passed to COMMAND.COM"; break;
case ENOEXEC: textstring = "Internal error -- exitting strongly suggested"; break;
case ENOMEM : textstring = "Out of memory -- cannot shell to DOS"; break;
case ENOENT : textstring = "Cannot find file COMMAND.COM -- initialize COMSPEC=[path]COMMAND.COM";
}
}
else if (!error) textstring = NULL;
return( textstring );
}
/*----------------------------------------------------------------------------
* Read, print, and increment the number of program executions.
* Print a comment, depending on the current execution number.
* Print the amount of available memory.
*
* "filename" - name of the text data file to read the number of program
* executions from (this number must be the first byte)
* - writes the incremented number of executions back to the file
* "title" - name of the program
* "author" - name of the author of the program
* "msgs" - one comment will be randomly chosen from this array of
* messages
* "msg_num" - number of messages present
*
* RETURNS:
* = file handle of the opened data file
*/
FILE *fil_count(char *filename, char *title, char *author, char *msgs[], word msg_num)
{
word counter; // usage counter
FILE *f; // file handle
DASHES; // print one line of dashes
if ((f = fopen(filename, "r+b")) == NULL) {
fprintf(stderr, ERR01, filename); // file could not be opened
exit(1); // abort with ERRORLEVEL 1
}
/* the first byte of the file is the usage counter -- get it */
fread(&counter, sizeof(counter), 1, f);
/* increment the counter and print the correct message */
if (counter++ == 0)
printf(MSG01, title, author); // print "first time used" message
else {
printf(MSG02, title, counter); // print number of program uses
if (counter != 1) // if uses do not equal 1 then
printf("s"); // pluralise
}
DOUBLESPACE;
/* write the incremented counter back to the file */
rewind(f); // go to beginning of file
fwrite(&counter, sizeof(counter), 1, f);
// the "fseek" is REQUIRED for the next "fread" to work???
fseek(f, 0L, SEEK_CUR);
printf(msgs[random(msg_num)], title);
printf(MSG03, coreleft(), title); // print amount of available memory
return( f ); // return file handle
}
/*----------------------------------------------------------------------------
* Read, print, and increment the number of program executions.
* Print to whom this program is registered.
* Print the amount of available memory.
*
* "filename" - name of the text data file to read the number of program
* executions from (this number must be the first byte)
* - writes the incremented number of executions back to the file
* "title" - name of the program
* "author" - name of the author of the program
* "registree" - name of the person to whom this program is registered
*
* RETURNS:
* = file handle of the opened data file
*/
FILE *fil_count_reg(char *filename, char *title, char *author, char *registree)
{
word counter; // usage counter
FILE *f; // file handle
DASHES; // print one line of dashes
if ((f = fopen(filename, "r+b")) == NULL) {
fprintf(stderr, ERR01, filename); // file could not be opened
exit(1); // abort with ERRORLEVEL 1
}
/* the first byte of the file is the usage counter -- get it */
fread(&counter, sizeof(counter), 1, f);
/* increment the counter and print the correct message */
if (counter++ == 0)
printf(MSG01, title, author); // print "first time used" message
else {
printf(MSG02, title, counter); // print number of program uses
if (counter != 1) // if uses do not equal 1 then
printf("s"); // pluralise
}
DOUBLESPACE;
/* write the incremented counter back to the file */
rewind(f); // go to beginning of file
fwrite(&counter, sizeof(counter), 1, f);
// the "fseek" is REQUIRED for the next "fread" to work???
fseek(f, 0L, SEEK_CUR);
if (!str_cmp(author, registree)) // if this is registered to author then
printf(MSG05, title, author); // print master copy message
else // else
printf(MSG04, title, registree); // print to whom this is registered
printf(MSG03, coreleft(), title); // print amount of available memory
return( f ); // return file handle
}
/*----------------------------------------------------------------------------
* Return the length of a file, in bytes.
*
* "f" - file handle of file to return length of
*
* RETURNS:
* = length of file in bytes
*/
long fil_len(FILE *f)
{
long curpos, length;
curpos = ftell(f); // save current position
fseek(f, 0L, SEEK_END); // go to EOF
length = ftell(f); // get EOF position relative to start
fseek(f, curpos, SEEK_SET); // go back to original position
return( length ); // return length of file, in bytes
}
/*----------------------------------------------------------------------------
* Read the next string from the current file position.
*
* "size" - initial size of text input buffer
* "f"